package com.winter.service;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.CompanyMapper;
import com.winter.mapper.CompanyPositionMapper;
import com.winter.mapper.DictEntryMapper;
import com.winter.mapper.FinBondMapper;
import com.winter.mapper.FinCpaperMapper;
import com.winter.mapper.FinDepositReceiptMapper;
import com.winter.mapper.FinFavInfoMapper;
import com.winter.mapper.FinManageMapper;
import com.winter.mapper.FinOnlineMapper;
import com.winter.mapper.FinUnderlineMapper;
import com.winter.mapper.IAnnMapper;
import com.winter.mapper.IApplyMapper;
import com.winter.mapper.IAttaMapper;
import com.winter.mapper.IeliteRelainfoMapper;
import com.winter.mapper.IindudyReportPolicyMapper;
import com.winter.mapper.NewCollectMapper;
import com.winter.mapper.NewGossipCommentMapper;
import com.winter.mapper.NewGossipLikeMapper;
import com.winter.mapper.NewGossipMapper;
import com.winter.mapper.NewLogonMapper;
import com.winter.mapper.NewOrgReportMapper;
import com.winter.mapper.NewVerificationMapper;
import com.winter.mapper.NoticeCustRelaMapper;
import com.winter.mapper.PersonalReleaseMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.mapper.UserMapper;
import com.winter.model.Company;
import com.winter.model.CompanyPosition;
import com.winter.model.DictEntry;
import com.winter.model.FinBond;
import com.winter.model.FinCpaper;
import com.winter.model.FinDepositReceipt;
import com.winter.model.FinFavInfo;
import com.winter.model.FinManage;
import com.winter.model.FinOnline;
import com.winter.model.FinUnderline;
import com.winter.model.IAnn;
import com.winter.model.IApply;
import com.winter.model.IAtta;
import com.winter.model.IeliteRelainfo;
import com.winter.model.IindudyReportPolicy;
import com.winter.model.NewCollect;
import com.winter.model.NewGossip;
import com.winter.model.NewGossipComment;
import com.winter.model.NewGossipLike;
import com.winter.model.NewLogon;
import com.winter.model.NewOrgReport;
import com.winter.model.NewVerification;
import com.winter.model.NoticeCustRela;
import com.winter.model.PersonalRelease;
import com.winter.model.User;
import com.winter.model.UserInfo;
import com.winter.model.vo.request.BindingPhoneRequestVo;
import com.winter.model.vo.request.CheckUpdatePwdCodeRequestVo;
import com.winter.model.vo.request.OtherLogonRequestVo;
import com.winter.model.vo.request.UserLoginRequestVo;
import com.winter.model.vo.request.UserRegisterRequestVo;
import com.winter.model.vo.response.LoginUserInfoResponseVo;
import com.winter.model.vo.response.ThinkTankCollectResponseVo;
import com.winter.model.vo.response.UserLoginResponseVo;
import com.winter.util.Constants;
import com.winter.util.MD5Utils;
import com.winter.util.P2pPubParaConstants;
import com.winter.util.Result;
import com.winter.util.ResultCode;
import com.winter.util.UUIDUitls;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
@Service
@Transactional
public class UserService {

    @Autowired
    private NewVerificationMapper newVerificationMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private IAttaMapper iAttaMapper;

    @Autowired
    private NoticeCustRelaMapper noticeCustRelaMapper;

    @Autowired
    private IApplyMapper iApplyMapper;

    @Autowired
    private NewGossipMapper newGossipMapper;

    @Autowired
    private NewGossipLikeMapper newGossipLikeMapper;

    @Autowired
    private NewGossipCommentMapper newGossipCommentMapper;

    @Autowired
    private NewCollectMapper newCollectMapper;

    @Autowired
    private NewOrgReportMapper newOrgReportMapper;

    @Autowired
    public IindudyReportPolicyMapper iindudyReportPolicyMapper;

    @Autowired
    private IAnnMapper iAnnMapper;

    @Autowired
    private IeliteRelainfoMapper ieliteRelainfoMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private DictEntryMapper dictEntryMapper;

    @Autowired
    private FinFavInfoMapper finFavInfoMapper;

    /**
     * 公司职位
     */
    @Autowired
    private CompanyPositionMapper companyPositionMapper;

    /**
     * 高级人才
     */
    @Autowired
    public PersonalReleaseMapper personalReleaseMapper;
    /**
     * 线上
     */
    @Autowired
    private FinOnlineService finOnlineService;
    /**
     * 线下
     */
    @Autowired
    private FinUnderlineService finUnderlineService;

    /**
     * 线下Mapper
     */
    @Autowired
    private FinUnderlineMapper finUnderlineMapper;
    /**
     * 理财
     */
    @Autowired
    private FinManageService finManageService;
    /**
     * 理财mapper
     */
    @Autowired
    private FinManageMapper finManageMapper;

    /**
     * 票据
     */
    @Autowired
    private FinCpaperService finCpaperService;

    /**
     * 票据
     */
    @Autowired
    private FinCpaperMapper finCpaperMapper;

    /**
     * 债券
     */
    @Autowired
    private FinBondService finBondService;

    /**
     * 债券
     */
    @Autowired
    private FinBondMapper finBondMapper;

    /**
     * 线上
     */
    @Autowired
    private FinOnlineMapper finOnlineMapper;

    /**
     * 存单
     */
    @Autowired
    private FinDepositReceiptMapper finDepositReceiptMapper;

    private String USER_TYPE_0 = "0";

    private String USER_TYPE_1 = "1";

    private String USER_TYPE_2 = "2";

    private String USER_TYPE_3 = "3";

    @Autowired
    private NewLogonMapper newLogonMapper;

    @Transactional
    public Result userRegister(UserRegisterRequestVo registerRequestVo, HttpServletRequest request) {
        if (registerRequestVo == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (registerRequestVo.getPhone() == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请填写手机号码");
        }
        if (Strings.isNullOrEmpty(registerRequestVo.getPassword())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请填写密码");
        }
        if (Strings.isNullOrEmpty(registerRequestVo.getAffirmPassword())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请填写确认密码");
        }
        if (Strings.isNullOrEmpty(registerRequestVo.getCode())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请填写验证码");
        }
        if (!registerRequestVo.getPassword()
                              .equals(registerRequestVo.getAffirmPassword())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "俩次密码不一致");
        }
        UserInfo userInfo1 = checkPhone(registerRequestVo.getPhone());
        if (userInfo1 != null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "此用户已被注册");
        }
        // 1代表注册验证码
        NewVerification newVerification = newVerificationMapper.selectByPhone(registerRequestVo.getPhone(), Constants.REGISTER);
        String check = checkCode(registerRequestVo.getCode(), newVerification);
        if (!Strings.isNullOrEmpty(check)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, check);
        }

        /*//  判断是否需要加入金融圈
        if (!Strings.isNullOrEmpty(registerRequestVo.getIsJoinJrg()) && registerRequestVo.getIsJoinJrg()
                                                                                         .equals("1")) {
            if (registerRequestVo.getUserCard() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请上传个人名片");
            }
        }*/
        addUser(registerRequestVo, request);
        return Result.success();
    }

    /**
     * 写入用户信息
     */
    private Long addUser(UserRegisterRequestVo registerRequestVo, HttpServletRequest request) {
        long userNo = System.currentTimeMillis();
        UserInfo userInfo = null;
        User user = new User();
        user.setCreatetime(new Date());
        user.setCreateUser(userNo + "");
        user.setIsvalid("1");
        user.setLastLoginTime(new Date());
        String passWord = MD5Utils.getMd5(registerRequestVo.getPassword());
        user.setPasswd(passWord);
        user.setSysId("3"); // 数据来源于app
        if (Strings.isNullOrEmpty(registerRequestVo.getAppName())) {
            user.setUserName(registerRequestVo.getPhone());
        } else {
            user.setUserName(registerRequestVo.getAppName());
        }
        user.setUserNo(userNo + "");
        user.setUpdatetime(new Date());
        user.setUpdateUser(userNo + "");
        user.setUserIp(getIpAddress(request));
        int insert = userMapper.insertSelective(user);

        if (insert == 1) {
            userInfo = new UserInfo();
            userInfo.setCreatetime(new Date());
            userInfo.setPasswdStrength(registerRequestVo.getPassword()
                                                        .length() + "");
            userInfo.setCreateUser(userNo + "");
            userInfo.setHasCata(Constants.FALSE + "");
            userInfo.setIsvip(Constants.FALSE + ""); // 默认非VIP
            userInfo.setUpdatetime(new Date());
            userInfo.setUserNo(userNo + "");
            userInfo.setCustMobile(registerRequestVo.getPhone());
            // 头像
            if (!Strings.isNullOrEmpty(registerRequestVo.getAvatar())) {
                userInfo.setCustImgUrl(registerRequestVo.getAvatar());
            }
            if (Strings.isNullOrEmpty(registerRequestVo.getAppName())) {
                userInfo.setUserName(registerRequestVo.getPhone());
            } else {
                userInfo.setUserName(registerRequestVo.getAppName());
            }
            userInfo.setUpdateUser(userNo + "");
            userInfo.setBeginDate(new Date());
            userInfo.setJoinJrg(null == registerRequestVo.getIsJoinJrg() ? "0" : registerRequestVo.getIsJoinJrg());
            userInfo.setSex("1");
            userInfo.setIsjrqmember(Constants.FALSE + "");
            userInfo.setAuditFlg(Constants.FALSE + "");
            userInfo.setIsvalid("1");
            userInfoMapper.insertSelective(userInfo);
            if (!Strings.isNullOrEmpty(registerRequestVo.getIsJoinJrg())) {
                registerRequestVo.getUserCard()
                                 .setCreatetime(new Date());
                registerRequestVo.getUserCard()
                                 .setCreateUser(registerRequestVo.getPhone());
                registerRequestVo.getUserCard()
                                 .setUpdatetime(new Date());
                registerRequestVo.getUserCard()
                                 .setRelaId(userInfo.getCustId());
                registerRequestVo.getUserCard()
                                 .setAttCata(P2pPubParaConstants.AttaType.CARD + "");
                registerRequestVo.getUserCard()
                                 .setUpdateUser(userNo + "");
                registerRequestVo.getUserCard()
                                 .setAttaSource("2");
                registerRequestVo.getUserCard()
                                 .setAttSeqNum(new BigDecimal(1));
                iAttaMapper.insertSelective(registerRequestVo.getUserCard());
            }
        }
        // 注册成功发送消息提示
        registerRequestVo.setPhone(userNo + "");
        addMsg(registerRequestVo, userInfo);
        return userInfo.getCustId();
    }

    /**
     * 注册完成 发送系统消息给用户
     */
    private void addMsg(UserRegisterRequestVo registerRequestVo, UserInfo userInfo) {
        NoticeCustRela record = new NoticeCustRela();
        record.setCreatetime(new Date());
        record.setCreateUser(registerRequestVo.getPhone());
        record.setIsRead(Constants.FALSE + "");
        record.setIsvalid(Constants.TRUE + "");
        record.setUpdatetime(new Date());
        record.setUpdatetime(new Date());
        record.setCustId(new BigDecimal(userInfo.getCustId()));
        record.setUpdateUser(registerRequestVo.getPhone());
        record.setContent("尊敬的" + registerRequestVo.getPhone() + "，您好！<br/>您注册的信息已成功提交，我们会在一个小时之内完成审核注册工作。（非上班时间审核工作可能会稍晚）请留意，谢谢！");
        int i = noticeCustRelaMapper.insertSelective(record);
    }


    public String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public UserInfo checkPhone(String phone) {
        UserInfo userInfo = userInfoMapper.checkPhone(phone);
        return userInfo;
    }

    /**
     * 登录
     */
    public Result login(UserLoginRequestVo userLoginRequestVo) {
        if (userLoginRequestVo == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (Strings.isNullOrEmpty(userLoginRequestVo.getLoginId())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入登录账号");

        }
        if (Strings.isNullOrEmpty(userLoginRequestVo.getPassword())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入密码");
        }
        String password = MD5Utils.getMd5(userLoginRequestVo.getPassword());
        UserLoginResponseVo login = userInfoMapper.login(userLoginRequestVo.getLoginId(), password);
        if (login == null) {
            return Result.failure(ResultCode.USER_LOGIN_ERROR);
        } else {
            Long id = login.getUserId();
            LoginUserInfoResponseVo loginUserInfoResponseVo = getLoginUserInfoResponseVo(id);
            return Result.success(loginUserInfoResponseVo);
        }
    }


    public Result verificationLogin(UserLoginRequestVo userLoginRequestVo) {
        UserLoginResponseVo login = userInfoMapper.verificationLogin(userLoginRequestVo.getLoginId());
        if (login == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "用户未注册");
        }
        // 3代表注册验证码
        NewVerification newVerification = newVerificationMapper.selectByPhone(userLoginRequestVo.getLoginId(), Constants.VERIFICATIONLOGIN);
        String check = checkCode(userLoginRequestVo.getCode(), newVerification);
        if (!Strings.isNullOrEmpty(check)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, check);
        }
        Long id = login.getUserId();
        LoginUserInfoResponseVo loginUserInfoResponseVo = getLoginUserInfoResponseVo(id);
        return Result.success(loginUserInfoResponseVo);
    }

    private LoginUserInfoResponseVo getLoginUserInfoResponseVo(Long id) {
        LoginUserInfoResponseVo loginUserInfoResponseVo = new LoginUserInfoResponseVo();
        // 获取用户详情信息
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
        loginUserInfoResponseVo.setUserNo(userInfo.getUserNo());
        loginUserInfoResponseVo.setAuditFlg(userInfo.getAuditFlg());
        loginUserInfoResponseVo.setCustImgUrl(userInfo.getCustImgUrl());
        loginUserInfoResponseVo.setCustMobile(userInfo.getCustMobile());
        loginUserInfoResponseVo.setIsjrqmember(userInfo.getIsjrqmember());
        loginUserInfoResponseVo.setIsvip(userInfo.getIsvip());
        loginUserInfoResponseVo.setUserId(userInfo.getCustId());
        loginUserInfoResponseVo.setUserName(userInfo.getUserName());
        loginUserInfoResponseVo.setCustEmail(userInfo.getCustEmail());
        loginUserInfoResponseVo.setSex(userInfo.getSex());
        loginUserInfoResponseVo.setWxNo(userInfo.getWxNo());
        getUserType(loginUserInfoResponseVo, userInfo);
        // 获取名片信息
        List<IAtta> iAtta = iAttaMapper.getIAtta(id, P2pPubParaConstants.AttaType.CARD);
        loginUserInfoResponseVo.setiAtta(iAtta);
        return loginUserInfoResponseVo;
    }

    private String checkCode(String code, NewVerification newVerification) {
        String result = null;
        if (newVerification == null) {
            result = "请先发送验证码";
        } else {
            if (!newVerification.getCode()
                                .equals(code)) {
                result = "验证码错误";
            }
            if (newVerification.getExpireTime()
                               .getTime() < System.currentTimeMillis()) {
                result = "验证码已过期";
            }
        }
        return result;
    }

    private void getUserType(LoginUserInfoResponseVo loginUserInfoResponseVo, UserInfo userInfo) {
        if (!Strings.isNullOrEmpty(userInfo.getAuditFlg())) {
            if (userInfo.getAuditFlg()
                        .equals(USER_TYPE_1)) {
                loginUserInfoResponseVo.setUserType(USER_TYPE_1);
                if (!Strings.isNullOrEmpty(userInfo.getIsjrqmember()) && userInfo.getIsjrqmember()
                                                                                 .equals(USER_TYPE_1)) {
                    loginUserInfoResponseVo.setUserType(USER_TYPE_2);
                }
                if (!Strings.isNullOrEmpty(userInfo.getIsvip()) && userInfo.getIsvip()
                                                                           .equals(USER_TYPE_1)) {
                    loginUserInfoResponseVo.setUserType(USER_TYPE_3);
                }
            } else {
                loginUserInfoResponseVo.setUserType(USER_TYPE_0);
            }
        } else {
            loginUserInfoResponseVo.setUserType(USER_TYPE_0);
        }
    }

    /**
     * 用户信息
     */
    public Result myProfile(Long userId) {
        if (userId != null) {
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
            if (userInfo != null) {
                LoginUserInfoResponseVo loginUserInfoResponseVo = new LoginUserInfoResponseVo();
                loginUserInfoResponseVo.setUserNo(userInfo.getUserNo());
                loginUserInfoResponseVo.setAuditFlg(userInfo.getAuditFlg());
                loginUserInfoResponseVo.setCustImgUrl(userInfo.getCustImgUrl());
                loginUserInfoResponseVo.setCustMobile(userInfo.getCustMobile());
                loginUserInfoResponseVo.setIsjrqmember(userInfo.getIsjrqmember());
                loginUserInfoResponseVo.setIsvip(userInfo.getIsvip());
                loginUserInfoResponseVo.setUserId(userInfo.getCustId());
                loginUserInfoResponseVo.setUserName(userInfo.getUserName());
                loginUserInfoResponseVo.setCustEmail(userInfo.getCustEmail());
                loginUserInfoResponseVo.setSex(userInfo.getSex());
                loginUserInfoResponseVo.setWxNo(userInfo.getWxNo());
                // 获取名片信息
                List<IAtta> iAtta = iAttaMapper.getIAtta(userId, P2pPubParaConstants.AttaType.CARD);
                loginUserInfoResponseVo.setiAtta(iAtta);
                getUserType(loginUserInfoResponseVo, userInfo);
                return Result.success(loginUserInfoResponseVo);
            }
        }
        return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
    }


    /**
     * 修改密码
     *
     * @param upPasswd 新密码
     */
    public Result updatePasswd(String userNo, String upPasswd) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "用户账号不能为空");
        }
        if (Strings.isNullOrEmpty(upPasswd)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "新密码不能为空");
        }
        User user = new User();
        user.setPasswd(MD5Utils.getMd5(upPasswd));
        user.setUserNo(userNo);
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i <= 0) {
            return Result.failure(ResultCode.USER_UP_PASSWD);
        }

        return Result.success("操作成功,下次登录生效");
    }

    /**
     * 更换手机号
     */
    public Result updateMobile(String userNo, String mobile, String code) {

        NewVerification newVerification = newVerificationMapper.selectByPhone(mobile, Constants.UPDATEPWD);
        String check = checkCode(code, newVerification);
        if (!Strings.isNullOrEmpty(check)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, check);
        }
        UserInfo info = new UserInfo();
        info.setCustMobile(mobile);
        info.setUserKey(userNo);
        userInfoMapper.updateByPrimaryKeySelective(info);
        return Result.success("更换成功");

    }

    /**
     * 我的报名
     */
    public Result myApply(String userNo, Integer pageSize, Integer pageNumber) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<IApply> iApplies = iApplyMapper.queryByUserId(userNo);
        if (iApplies.size() > 0) {
            for (IApply iApply : iApplies) {
                Long relaId = iApply.getRelaId();
                IAnn iAnn = iAnnMapper.selectByPrimaryKey(relaId);
                if (iAnn != null) {
                    iApply.setCreationAt(iAnn.getCreatetime());
                    iApply.setRelaTitle(iAnn.getAnntitle());
                }
            }
        }
        PageInfo<IApply> info = new PageInfo<>(iApplies);
        return Result.success(info);
    }

    /**
     *
     */
    public Result myGossip(Long userId, Integer pageSize, Integer pageNumber) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (userId == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<NewGossip> newGossips = newGossipMapper.myGossip(userId);
        newGossips.forEach(f -> {
            //  是否有点赞
            NewGossipLike byUserAndGossip = newGossipLikeMapper.getByUserAndGossip(f.getId(), userId);
            if (byUserAndGossip != null) {
                f.setIsLike(1);
            } else {
                f.setIsLike(0);
            }
            // 点赞总数
            List<NewGossipLike> likes = newGossipLikeMapper.getByGossipId(f.getId());
            f.setLikeCount(likes.size());
            // 评论总数
            List<NewGossipComment> comments = newGossipCommentMapper.getByGossipId(f.getId());
          /*  comments.forEach(c -> {
                NewGossipComment byReply = newGossipCommentMapper.getByReply(c.getId());
                c.setNewGossipReply(byReply);
                f.setNewGossipComment(c);
            });*/
            f.setCommentCount(comments.size());

        });
        PageInfo<NewGossip> pageInfo = new PageInfo<>(newGossips);
        return Result.success(pageInfo);
    }

    public Result myCollectThinkTank(Long userId, Integer pageSize, Integer pageNumber, String type) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (userId == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<NewCollect> newCollects = newCollectMapper.selectByUserId(userId, type);
        List<ThinkTankCollectResponseVo> list = new ArrayList<>();
        if (newCollects.size() > 0) {
            newCollects.forEach(f -> {
                if (f.getSourceType()
                     .equals("1")) {
                    NewOrgReport newOrgReport = newOrgReportMapper.selectByPrimaryKey(Long.parseLong(f.getSourceId()));
                    ThinkTankCollectResponseVo collectResponseVo = new ThinkTankCollectResponseVo();
                    collectResponseVo.setCreatedAt(newOrgReport.getCreatetime());
                    collectResponseVo.setId(newOrgReport.getId() + "");
                    collectResponseVo.setTitle(newOrgReport.getTitle());
                    collectResponseVo.setType("5");
                    collectResponseVo.setTypename("机构研报");
                    collectResponseVo.setPictureAddress(newOrgReport.getImgpath());
                    list.add(collectResponseVo);
                }
                if (f.getSourceType()
                     .equals("2") || f.getSourceType()
                                      .equals("3") || f.getSourceType()
                                                       .equals("4") || f.getSourceType()
                                                                        .equals("5")) {
                    IAnn iAnn = iAnnMapper.selectByPrimaryKey(Long.parseLong(f.getSourceId()));
                    ThinkTankCollectResponseVo collectResponseVo = new ThinkTankCollectResponseVo();
                    if (iAnn.getAnnType()
                            .equals("1")) {
                        collectResponseVo.setTypename("精品课程");
                        collectResponseVo.setType("1");
                    }
                    if (iAnn.getAnnType()
                            .equals("2")) {
                        collectResponseVo.setTypename("同业沙龙");
                        collectResponseVo.setType("2");
                    }
                    if (iAnn.getAnnType()
                            .equals("3")) {
                        collectResponseVo.setTypename("高级论坛");
                        collectResponseVo.setType("3");
                    }
                    if (iAnn.getAnnType()
                            .equals("4")) {
                        collectResponseVo.setTypename("专家讲坛");
                        collectResponseVo.setType("4");
                    }
                    collectResponseVo.setTitle(iAnn.getAnntitle());
                    collectResponseVo.setPictureAddress(iAnn.getPicPath());
                    collectResponseVo.setCreatedAt(iAnn.getCreatetime());
                    collectResponseVo.setId(iAnn.getId() + "");
                    list.add(collectResponseVo);
                }
                if (f.getSourceType()
                     .equals("6")) {
                    NewGossip newGossip = newGossipMapper.selectByPrimaryKey(f.getSourceId());
                    List<NewGossipLike> byGossipId = newGossipLikeMapper.getByGossipId(f.getSourceId());
                    List<NewGossipComment> byGossipId1 = newGossipCommentMapper.getByGossipId(f.getSourceId());
                    if (newGossip != null) {
                        ThinkTankCollectResponseVo collectResponseVo = new ThinkTankCollectResponseVo();
                        collectResponseVo.setCreatedAt(newGossip.getCreatedAt());
                        collectResponseVo.setId(newGossip.getId() + "");
                        collectResponseVo.setTitle(newGossip.getContent());
                        collectResponseVo.setType("6");
                        collectResponseVo.setTypename("八卦");
                        collectResponseVo.setAvatar(newGossip.getUserLogo());
                        collectResponseVo.setIsAnonymity(newGossip.getIsAnonymity());
                        collectResponseVo.setUserName(newGossip.getUserName());
                        collectResponseVo.setBrowseNum(newGossip.getBrowseNum());
                        collectResponseVo.setLikeNum(byGossipId.size());
                        collectResponseVo.setCommentNum(byGossipId1.size());
                        collectResponseVo.setPictureAddress(newGossip.getPicture());
                        list.add(collectResponseVo);
                    }

                }
            });
        }
        PageInfo<NewCollect> pageInfo = new PageInfo<>(newCollects);
        return Result.success(list, pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPageNum());
    }

    public Result checkUpdatePwdCode(CheckUpdatePwdCodeRequestVo checkUpdatePwdCodeRequestVo) {

        if (checkUpdatePwdCodeRequestVo != null) {
            if (checkUpdatePwdCodeRequestVo.getPhone() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入手机号码");
            }
            if (checkUpdatePwdCodeRequestVo.getCode() == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入验证码");
            }
            NewVerification newVerification = newVerificationMapper.selectByPhone(checkUpdatePwdCodeRequestVo.getPhone(), Constants.REGISTER);
            if (newVerification == null) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请先发送验证码");
            } else {
                if (!newVerification.getCode()
                                    .equals(checkUpdatePwdCodeRequestVo.getCode())) {
                    return Result.failure(ResultCode.PARAM_IS_BLANK, "验证码错误");
                }
                if (newVerification.getExpireTime()
                                   .getTime() < System.currentTimeMillis()) {
                    return Result.failure(ResultCode.PARAM_IS_BLANK, "验证码已过期");
                }
            }
            return Result.success();
        } else {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
    }

    /**
     * 删除报名
     */
    public Result deleteApply(Long id) {
        int i = iApplyMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.success();
        } else {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN, "删除失败");
        }
    }

    /**
     * 我的公司收藏
     */
    public Result myCollectCompany(String userNo, Integer pageSize, Integer pageNumber) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        // 0 = 公司圈
        List<IeliteRelainfo> list = ieliteRelainfoMapper.getList(userNo, "0");
        PageInfo<IeliteRelainfo> pageInfo = new PageInfo<>(list);
        if (list.size() > 0) {
            list.forEach(f -> {
                Company company = companyMapper.selectByPrimaryKey(f.getRelaId());
                DictEntry dictEntry = dictEntryMapper.get("DC_INDU_CLASSIFY", company.getCompanyIndustry());
                if (dictEntry != null) {
                    company.setCompanyIndustry(dictEntry.getDictname());
                }
                DictEntry dictEntry2 = dictEntryMapper.get("DC_COM_CLASSIFY", company.getCompanyNature());
                if (dictEntry2 != null) {
                    company.setCompanyNature(dictEntry2.getDictname());
                }
                f.setCompany(company);
            });
        }
        return Result.success(pageInfo);
    }

    public Result uploadCard(IAtta iAtta) {
        if (iAtta != null) {
            if (Strings.isNullOrEmpty(iAtta.getCreateUser())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            if (Strings.isNullOrEmpty(iAtta.getAttPath())) {
                return Result.failure(ResultCode.PARAM_IS_BLANK, "请先上传名片");
            }
            iAtta.setAttSeqNum(new BigDecimal(1));
            iAtta.setUpdateUser(iAtta.getCreateUser());
            iAtta.setUpdatetime(new Date());
            iAtta.setAttaSource("2");
            iAtta.setAttCata(P2pPubParaConstants.AttaType.CARD + "");
            iAtta.setAttSrcTitle("用户名片");
            iAtta.setAttTitle("用户名片");
            iAtta.setCreatetime(new Date());
            iAttaMapper.insertSelective(iAtta);
        }
        return Result.success();
    }

    public Result updateHead(UserInfo userInfo) {
        if (userInfo.getCustId() == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if (i > 0) {
            return Result.success();

        }
        return Result.failure(ResultCode.PARAM_IS_BLANK, "修改失败");
    }


    public Result myCollect(String userNo, Integer type, Integer pageSize, Integer pageNumber) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        //PageHelper.startPage(pageNumber, pageSize, false);
        List<Map<String, Object>> list = Lists.newArrayList();
        List<FinFavInfo> finFavInfo = finFavInfoMapper.myCollect(userNo, type);
        for (FinFavInfo f : finFavInfo) {
            Long relaId = f.getRelaId();
            //  收藏 -职位 -简历
            if (type == 8) {
                if (f.getRelaChildType()
                     .equals("1")) {
                    CompanyPosition companyPosition = companyPositionMapper.selectById(relaId);
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("jobName", companyPosition.getJobFunction());
                    Company company = companyMapper.selectByPrimaryKey(companyPosition.getCompanyreleaseId());
                    map.put("name", company.getCompanyName());
                    map.put("type", "高端职位");
                    map.put("date", companyPosition.getCreatetime());
                    map.put("id", companyPosition.getId());
                    map.put("relaType", 8);
                    map.put("relaChildType", 1);
                    list.add(map);
                }
                if (f.getRelaChildType()
                     .equals("2")) {
                    PersonalRelease personalRelease = personalReleaseMapper.selectByPrimaryKey(relaId);
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("jobName", personalRelease.getShowName() + "(意向职位:" + personalRelease.getHirejob() + ")");
                    map.put("name", "工作年限:" + personalRelease.getWorklife() + "年");
                    map.put("type", "高端人才");
                    map.put("date", personalRelease.getCreatetime());
                    map.put("id", personalRelease.getId());
                    map.put("relaType", 8);
                    map.put("relaChildType", 2);
                    list.add(map);
                }
            }
            if (type == 2) {
                //业界动态  =1; //研究分析 =2; //政策法规 =3;//干货精选 =4;  早餐 or 日历= 5; //好文  = 7;
                IindudyReportPolicy reportPolicy = iindudyReportPolicyMapper.selectByPrimaryKey(relaId);
                if (reportPolicy != null) {
                    HashMap<String, Object> map = new HashMap<>();
                    if (reportPolicy.getTypeflag()
                                    .equals("1")) {
                        map.put("type", "业界动态");
                        map.put("relaType", 2);
                        map.put("relaChildType", 1);
                    }
                    if (reportPolicy.getTypeflag()
                                    .equals("2")) {
                        map.put("type", "研究分析");
                        map.put("relaType", 2);
                        map.put("relaChildType", 2);
                    }
                    if (reportPolicy.getTypeflag()
                                    .equals("3")) {
                        map.put("type", "政策法规");
                        map.put("relaType", 2);
                        map.put("relaChildType", 3);
                    }
                    if (reportPolicy.getTypeflag()
                                    .equals("7")) {
                        map.put("type", "好文推荐");
                        map.put("relaType", 2);
                        map.put("relaChildType", 7);
                    }
                    if (reportPolicy.getTypeflag()
                                    .equals("5")) {
                        map.put("type", "每日早餐");
                        map.put("relaType", 2);
                        map.put("relaChildType", 5);
                    }
                    if (reportPolicy.getTypeflag()
                                    .equals("4")) {
                        map.put("type", "干货精选");
                        map.put("relaType", 2);
                        map.put("relaChildType", 4);
                    }
                    map.put("title", reportPolicy.getTitle());
                    map.put("date", reportPolicy.getCreatetime());
                    map.put("id", reportPolicy.getId());
                    list.add(map);
                }
            }
            // 线上
            if (type == 1) {
                if (f.getRelaChildType()
                     .equals("1")) {
                    FinOnline finOnline = finOnlineMapper.selectByPrimaryKey(relaId);
                    if (finOnline != null) {
                        finOnlineService.getTypeName(finOnline);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("finOnline", finOnline);
                        map.put("type", "线上");
                        map.put("relaType", 1);
                        map.put("relaChildType", 1);
                        list.add(map);
                    }
                }
                if (f.getRelaChildType()
                     .equals("2")) {
                    FinUnderline finUnderline = finUnderlineMapper.selectByPrimaryKey(relaId);
                    if (finUnderline != null) {
                        finUnderlineService.getTypeName(finUnderline);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("finUnderline", finUnderline);
                        map.put("type", "线下");
                        map.put("relaType", 1);
                        map.put("relaChildType", 2);
                        list.add(map);
                    }
                }
                if (f.getRelaChildType()
                     .equals("3")) {
                    FinManage finManage = finManageMapper.selectByPrimaryKey(relaId);
                    if (finManage != null) {
                        finManageService.getTypeName(finManage);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("finManage", finManage);
                        map.put("type", "理财");
                        map.put("relaType", 1);
                        map.put("relaChildType", 3);
                        list.add(map);
                    }
                }
                if (f.getRelaChildType()
                     .equals("4")) {
                    FinCpaper finCpaper = finCpaperMapper.selectByPrimaryKey(relaId);
                    if (finCpaper != null) {
                        finCpaperService.getTypeName(finCpaper);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("finCpaper", finCpaper);
                        map.put("type", "票据");
                        map.put("relaType", 1);
                        map.put("relaChildType", 4);
                        list.add(map);
                    }
                }
                if (f.getRelaChildType()
                     .equals("6")) {
                    FinBond finBond = finBondMapper.selectByPrimaryKey(relaId);
                    if (finBond != null) {
                        finBondService.getTypeName(finBond);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("finCpaper", finBond);
                        map.put("type", "债券");
                        map.put("relaType", 1);
                        map.put("relaChildType", 6);
                        list.add(map);
                    }
                }
                if (f.getRelaChildType()
                     .equals("7")) {
                    FinDepositReceipt finDepositReceipt = finDepositReceiptMapper.selectByPrimaryKey(relaId);
                    HashMap<String, Object> map = new HashMap<>();
                    if (finDepositReceipt != null && finDepositReceipt.getDirect()
                                                                      .equals("1")) {
                        finDepositReceipt.setDirect("出");
                    } else {
                        finDepositReceipt.setDirect("收");
                    }
                    map.put("finDepositReceipt", finDepositReceipt);
                    map.put("type", "存单");
                    map.put("relaType", 1);
                    map.put("relaChildType", 7);
                    list.add(map);
                }
            }
        }
        //PageInfo<FinFavInfo> pageInfo = new PageInfo<>(finFavInfo);
        //pageInfo.setList();
        return Result.success(list);
    }

    /**
     * 我发布的报价
     */
    public Result my_pushQuote(String userNo) {
        // 线上
        List<FinOnline> finOnlines = finOnlineMapper.queryUserNo(userNo);
        List<Map<String, Object>> list = Lists.newArrayList();
        if (finOnlines.size() > 0) {
            finOnlines.forEach(finOnline -> {
                finOnlineService.getTypeName(finOnline);
                HashMap<String, Object> map = new HashMap<>();
                map.put("方向", finOnline.getDirect());
                map.put("模式", finOnline.getFundMode());
                map.put("对手", finOnline.getCouParty());
                map.put("联系方式", finOnline.getContact());
                map.put("date", finOnline.getCreatetime());
                map.put("id", finOnline.getId());
                map.put("type", "线上");
                map.put("auditing", finOnline.getAuditing());
                map.put("stas", finOnline.getStas());
                map.put("relaType", 1);
                map.put("relaChildType", 1);
                map.put("finOnline", finOnline);
                list.add(map);
            });
        }
        // 线下
        List<FinUnderline> finUnderlines = finUnderlineMapper.queryUserNo(userNo);
        if (finUnderlines.size() > 0) {
            finUnderlines.forEach(finUnderline -> {
                finUnderlineService.getTypeName(finUnderline);
                HashMap<String, Object> map = new HashMap<>();
                map.put("方向", finUnderline.getDirect());
                map.put("种类", finUnderline.getFundMode());
                map.put("币种", finUnderline.getCcy());
                map.put("联系方式", finUnderline.getContact());
                map.put("date", finUnderline.getCreatetime());
                map.put("id", finUnderline.getId());
                map.put("type", "线下");
                map.put("auditing", finUnderline.getAuditing());
                map.put("stas", finUnderline.getStas());
                map.put("relaType", 1);
                map.put("relaChildType", 2);
                map.put("finUnderline", finUnderline);
                list.add(map);
            });
        }
        List<FinManage> finManages = finManageMapper.queryUserNo(userNo);
        if (finManages.size() > 0) {
            finManages.forEach(finManage -> {
                finManageService.getTypeName(finManage);
                HashMap<String, Object> map = new HashMap<>();
                map.put("方向", finManage.getDirect());
                map.put("保本性质", finManage.getFundMode());
                map.put("保函", finManage.getLoi());
                map.put("联系方式", finManage.getContact());
                map.put("date", finManage.getCreatetime());
                map.put("id", finManage.getId());
                map.put("type", "理财");
                map.put("auditing", finManage.getAuditing());
                map.put("stas", finManage.getStas());
                map.put("relaType", 1);
                map.put("relaChildType", 3);
                map.put("finManage", finManage);
                list.add(map);
            });
        }
        List<FinCpaper> finCpapers = finCpaperMapper.queryUserNo(userNo);
        if (finCpapers.size() > 0) {
            finCpapers.forEach(finCpaper -> {
                finCpaperService.getTypeName(finCpaper);
                HashMap<String, Object> map = new HashMap<>();
                map.put("方向", finCpaper.getDirect());
                map.put("模式", finCpaper.getTradeMode());
                map.put("形式", finCpaper.getFundType());
                map.put("date", finCpaper.getCreatetime());
                map.put("联系方式", finCpaper.getContact());
                map.put("id", finCpaper.getId());
                map.put("type", "票据");
                map.put("auditing", finCpaper.getAuditing());
                map.put("stas", finCpaper.getStas());
                map.put("relaType", 1);
                map.put("relaChildType", 4);
                map.put("finCpaper", finCpaper);
                list.add(map);
            });
        }
        List<FinBond> finBonds = finBondMapper.queryUserNo(userNo);
        if (finBonds.size() > 0) {
            finBonds.forEach(finBond -> {
                finBondService.getTypeName(finBond);
                HashMap<String, Object> map = new HashMap<>();
                map.put("方向", finBond.getDirect());
                map.put("券种", finBond.getBondType());
                map.put("主体评级", finBond.getIssRating());
                map.put("联系方式", finBond.getContact());
                map.put("date", finBond.getCreatetime());
                map.put("id", finBond.getId());
                map.put("type", "债券");
                map.put("auditing", finBond.getAuditing());
                map.put("stas", finBond.getStas());
                map.put("relaType", 1);
                map.put("relaChildType", 6);
                map.put("finBond", finBond);
                list.add(map);
            });
        }
        List<FinDepositReceipt> finDepositReceipts = finDepositReceiptMapper.queryUserNo(userNo);
        if (finDepositReceipts.size() > 0) {
            finDepositReceipts.forEach(finDepositReceipt -> {
                HashMap<String, Object> map = new HashMap<>();
                if (finDepositReceipt.getDirect()
                                     .equals("1")) {
                    map.put("方向", "出");
                } else {
                    map.put("方向", "收");
                }
                map.put("评级", finDepositReceipt.getRating());
                map.put("公司简称", finDepositReceipt.getUserCom());
                map.put("联系方式", finDepositReceipt.getContact());
                map.put("id", finDepositReceipt.getId());
                map.put("date", finDepositReceipt.getCreateTime());
                map.put("type", "存单");
                map.put("auditing", finDepositReceipt.getAuditing());
                map.put("stas", finDepositReceipt.getStas());
                map.put("relaType", 1);
                map.put("relaChildType", 7);
                map.put("finDepositReceipt", finDepositReceipt);
                list.add(map);
            });
        }
        return Result.success(list);
    }

    /**
     * 删除我的报价
     */
    public Result deletePushQuote(Long id, Integer type) {
        if (type == 1) {
            finOnlineMapper.deleteByPrimaryKey(id);
        }
        if (type == 2) {
            finUnderlineMapper.deleteByPrimaryKey(id);
        }
        if (type == 3) {
            finManageMapper.deleteByPrimaryKey(id);
        }
        if (type == 4) {
            finCpaperMapper.deleteByPrimaryKey(id);
        }
        if (type == 6) {
            finBondMapper.deleteByPrimaryKey(id);
        }
        if (type == 7) {
            finDepositReceiptMapper.deleteByPrimaryKey(id);
        }
        // 删除收藏
        finFavInfoMapper.delete(id + "", "1", type + "", null);
        return Result.success();
    }

    /**
     * 系统消息
     */
    public Result message(String userNo, Integer pageSize, Integer pageNumber, Long userId) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (userId == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<NoticeCustRela> noticeCustRelas = noticeCustRelaMapper.queryList(userNo, userId);
        PageInfo<NoticeCustRela> pageInfo = new PageInfo<>(noticeCustRelas);
        return Result.success(pageInfo);
    }

    /**
     * 标记已读
     */
    public Result messageIsRead(Long id) {
        noticeCustRelaMapper.updateStatus(id);
        return Result.success();
    }

    public Result myUnreadNum(Long userId) {
        List<NoticeCustRela> noticeCustRelas = noticeCustRelaMapper.myUnreadNum(userId);
        return Result.success(noticeCustRelas.size());
    }

    /**
     * 第三方登录
     */
    public Result otherLogon(OtherLogonRequestVo otherLogonRequestVo) {

        NewLogon newLogon = newLogonMapper.selectByOpenId(otherLogonRequestVo.getOpenId(), otherLogonRequestVo.getAppType());
        if (newLogon == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请先进行绑定");
        }
        LoginUserInfoResponseVo loginUserInfoResponseVo = getLoginUserInfoResponseVo(newLogon.getUserId());
        return Result.success(loginUserInfoResponseVo);
    }

    /**
     * 第三方登录是否绑定
     */
    public Result isBinding(OtherLogonRequestVo otherLogonRequestVo) {
        if (otherLogonRequestVo == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        NewLogon newLogon = newLogonMapper.selectByOpenId(otherLogonRequestVo.getOpenId(), otherLogonRequestVo.getAppType());
        if (newLogon == null) {
            return Result.failure(ResultCode.FAILURE.code(), "请选择绑定号码");
        } else {
            return Result.success();
        }
    }

    /**
     * 第三方绑定手机号码
     */
    public Result binding(BindingPhoneRequestVo bindingPhoneRequestVo, HttpServletRequest request) {
        if (bindingPhoneRequestVo == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (Strings.isNullOrEmpty(bindingPhoneRequestVo.getPhone())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入绑定手机号码");
        }
        if (Strings.isNullOrEmpty(bindingPhoneRequestVo.getCode())) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请输入验证码");
        }
        NewVerification newVerification = newVerificationMapper.selectByPhone(bindingPhoneRequestVo.getPhone(), Constants.VERIFICATIONLOGIN);
        if (newVerification == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请先发送验证码");
        }
        if (newVerification.getCode()
                           .equals(bindingPhoneRequestVo.getCode())) {
            UserInfo userInfo = checkPhone(bindingPhoneRequestVo.getPhone());
            // 手机号码未被绑定 或者未被注册
            if (userInfo == null) {
                UserRegisterRequestVo registerRequestVo = new UserRegisterRequestVo();
                registerRequestVo.setPhone(bindingPhoneRequestVo.getPhone());
                registerRequestVo.setPassword("1234567890");
                registerRequestVo.setAvatar(bindingPhoneRequestVo.getAvatar());
                registerRequestVo.setAppName(bindingPhoneRequestVo.getAppName());
                Long userId = addUser(registerRequestVo, request);
                insertNewLogon(bindingPhoneRequestVo, userId);
                LoginUserInfoResponseVo loginUserInfoResponseVo = getLoginUserInfoResponseVo(userId);
                return Result.success(loginUserInfoResponseVo);
            } else {
                // 手机号码已经被注册
                insertNewLogon(bindingPhoneRequestVo, userInfo.getCustId());
                LoginUserInfoResponseVo loginUserInfoResponseVo = getLoginUserInfoResponseVo(userInfo.getCustId());
                return Result.success(loginUserInfoResponseVo);
            }
        }
        return Result.failure(ResultCode.PARAM_IS_BLANK, "验证码错误");
    }

    /**
     * 新增第三方记录
     */
    private void insertNewLogon(BindingPhoneRequestVo bindingPhoneRequestVo, Long userId) {
        NewLogon newLogon = new NewLogon();
        newLogon.setAppId(bindingPhoneRequestVo.getOpenId());
        newLogon.setAppName(bindingPhoneRequestVo.getAppName());
        newLogon.setAppType(bindingPhoneRequestVo.getType());
        newLogon.setUserId(userId);
        newLogon.setCreatedAt(new Date());
        newLogon.setId(UUIDUitls.uuid());
        newLogon.setUserPhone(bindingPhoneRequestVo.getPhone());
        newLogonMapper.insertSelective(newLogon);
    }
}